package com.kuqi.service.impl;

import com.kuqi.domain.News;
import com.kuqi.domain.PageVo;
import com.kuqi.domain.Vo;
import com.kuqi.mapper.NewsMapper;
import com.kuqi.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author:kuqi
 * @createTime:2021/11/16 8:54
 * @description:
 */

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public void save(News news) {
        newsMapper.save(news);
    }

    @Override
    public void updateNews(News news) {
        newsMapper.update(news);
    }

    @Override
    public void deleteNewsById(Integer id) {
        newsMapper.deleteNewsById(id);
    }

    @Override
    public Vo<News> getNewsListByPage(PageVo pageVo) {
        /**
         * 分页的逻辑层要查询两个
         * 1.查询新闻列表总记录数
         * 2.查询分页记录条数
         */
        int newsTotal = newsMapper.getNewsTotal();
        List<News> newsList = newsMapper.getNewsListByPage(pageVo);

        //将数据封装到
        Vo<News> newsVo = new Vo<>();
        newsVo.setTotal(newsTotal);
        newsVo.setDataList(newsList);

        return newsVo;
    }

    @Override
    public News getNewsById(Integer id) {
        News news = newsMapper.getNewsById(id);
        return news;
    }
}
